﻿using UnityEngine;
using UnityEngine.UI;

namespace Watermelon
{
    public class UIGame : UIPage
    {
        [SerializeField] FloatingJoystick joystick;

        [Header("Fade")]
        [SerializeField] RectTransform fadeMaskRectTransform;
        [SerializeField] Image fadeImage;

        [Space]
        [SerializeField] CurrenciesUIController currenciesUIController;

        [Space]
        [SerializeField] GameObject dropButtonObject;

        [Header("World Ad Button")]
        [SerializeField] TableZoneAdButtonBehaviour tableZoneAdButtonBehaviour;

        public FloatingJoystick Joystick => joystick;

        public CurrenciesUIController CurrenciesUIController => currenciesUIController;

        private TweenCase fadeTweenCase;

        public override void Hide()
        {
            HideImmediately();
        }

        public override void HideImmediately()
        {
            canvas.enabled = false;
            isPageDisplayed = false;
        }

        public override void Show()
        {
            ShowImmediately();
        }

        public override void ShowImmediately()
        {
            if (isPageDisplayed)
                return;

            isPageDisplayed = true;
            canvas.enabled = true;
        }

        public void EnableFadeImage()
        {
            fadeMaskRectTransform.gameObject.SetActive(true);
            fadeMaskRectTransform.sizeDelta = Vector2.zero;
        }

        public void EnableFadeImage(float duration, Ease.Type easing, Tween.TweenCallback onComplete)
        {
            if (fadeTweenCase != null && !fadeTweenCase.isCompleted)
                fadeTweenCase.Kill();

            fadeMaskRectTransform.gameObject.SetActive(true);
            fadeMaskRectTransform.sizeDelta = new Vector2(2500, 2500);

            fadeTweenCase = fadeMaskRectTransform.DOSize(Vector2.zero, duration).SetEasing(easing).OnComplete(onComplete);
        }

        public void DisableFadeImage(float duration, Ease.Type easing, Tween.TweenCallback callback)
        {
            if (fadeTweenCase != null && !fadeTweenCase.isCompleted)
                fadeTweenCase.Kill();

            fadeMaskRectTransform.gameObject.SetActive(true);
            fadeMaskRectTransform.sizeDelta = new Vector2(0, 0);

            fadeTweenCase = fadeMaskRectTransform.DOSize(new Vector2(2500, 2500), duration).SetEasing(easing).OnComplete(delegate
            {
                fadeImage.gameObject.SetActive(false);
            }).OnComplete(callback);
        }

        #region Drop Button
        public void DropButton()
        {
#if MOREMOUNTAINS_NICEVIBRATIONS
        if (AudioController.IsVibrationEnabled())
            Lofelt.NiceVibrations.HapticPatterns.PlayPreset(Lofelt.NiceVibrations.HapticPatterns.PresetType.LightImpact);
#endif
            AudioController.PlaySound(AudioController.Sounds.buttonSound);

            PlayerBehavior.DropItemsAndAnimals();

            SetDropButtonState(false);
        }

        public void SetDropButtonState(bool state)
        {
            dropButtonObject.SetActive(state);
        }
        #endregion

        public void ActivateZoneAdButton(IPurchaseObject purchaseObject)
        {
            tableZoneAdButtonBehaviour.Initialise(purchaseObject);
        }

        public void DisableZoneAdButton()
        {
            tableZoneAdButtonBehaviour.Hide();
        }
    }
}